using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace InvadersEx01
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class InvadersEx01Game : Microsoft.Xna.Framework.Game
    {
        private const float k_EnemyWidth = 32;

        private GraphicsDeviceManager m_Graphics;
        private SpriteBatch m_SpriteBatch;

        private Texture2D m_BackgroundTexture;
        private Vector2 m_BackgroundPosition;

        private Texture2D m_Enemy1Texture;
        private Texture2D m_Enemy2Texture;
        private Texture2D m_Enemy3Texture;

        private Enemies m_Enemies;

        private Texture2D m_ShipTexture;
        private Vector2 m_ShipPosition;

        private bool m_GameRunning;

        public InvadersEx01Game()
        {
            m_Graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            this.Window.Title = "Invaders Ex01";
            m_Enemies = new Enemies(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
            m_GameRunning = true;

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            m_SpriteBatch = new SpriteBatch(GraphicsDevice);

            m_BackgroundTexture = Content.Load<Texture2D>(@"Sprites\BG_Space01_1024x768");
            m_ShipTexture = Content.Load<Texture2D>(@"Sprites\Ship01_32x32");
            m_Enemy1Texture = Content.Load<Texture2D>(@"Sprites\Enemy1_32x32");
            m_Enemy2Texture = Content.Load<Texture2D>(@"Sprites\Enemy2_32x32");
            m_Enemy3Texture = Content.Load<Texture2D>(@"Sprites\Enemy3_32x32");

            initBackgroundPosition();
            initShipPosition();
        }

        private void initBackgroundPosition()
        {
            m_BackgroundPosition = new Vector2(0, 0);
        }

        private void initShipPosition()
        {
            m_ShipPosition = new Vector2(
                (GraphicsDevice.Viewport.Width / 2) - (m_ShipTexture.Width / 2),
                GraphicsDevice.Viewport.Height - m_ShipTexture.Height - 15);
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                this.Exit();
            }

            if (m_GameRunning)
            {
                m_GameRunning = !m_Enemies.MoveEnemies((float)gameTime.ElapsedGameTime.TotalSeconds);
            }

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            m_SpriteBatch.Begin();
            m_SpriteBatch.Draw(m_BackgroundTexture, m_BackgroundPosition, Color.White);
            m_SpriteBatch.Draw(m_ShipTexture, m_ShipPosition, Color.White);

            foreach (Enemy enemy in m_Enemies.EnemiesList)
            {
                switch (enemy.EnemyType)
                {
                    case eEnemyType.Enemy1:
                        m_SpriteBatch.Draw(m_Enemy1Texture, enemy.Position, Color.White);
                        break;
                    case eEnemyType.Enemy2:
                        m_SpriteBatch.Draw(m_Enemy2Texture, enemy.Position, Color.White);
                        break;
                    case eEnemyType.Enemy3:
                        m_SpriteBatch.Draw(m_Enemy3Texture, enemy.Position, Color.White);
                        break;
                }
            }

            m_SpriteBatch.End();

            base.Draw(gameTime);
        }
    }
}